// 学习swift语言 必备

import UIKit

var str = "学习swift语言"


let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}
//var reversed = sort(names, backwards)

extension Array {
    var decompose : (head: Element, tail: [Element])? {
        return (count > 0) ? (self[0], Array(self[1..<count])) : nil
    }
}

func qsortDemo(input: [Int]) -> [Int] {
    if let (pivot, rest) = input.decompose {
        let lesser = rest.filter { $0 < pivot }
        let greater = rest.filter { $0 >= pivot }
        return qsortDemo(lesser) + [pivot] + qsortDemo(greater)
    } else {
        return []
    }
}

qsortDemo([3,4,1,6,7])


let numarr = [3,4,7,9,10]

func backsort(a1:NSInteger , a2:NSInteger) ->Bool{

    return a1 > a2
}

//var newsortarr = sorted(numarr,backsort)

print("使用元组")
let (first,second) = (12,"knight.lu")

print("使用元组打印 第一个数\(first) 第二个数\(second)")

//let a = backsort(0, 1)
//println(a)

func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backwards ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
    print("\(currentValue)... ")
    currentValue = moveNearerToZero(currentValue)
}
print("zero!")

print("***********")

func returnFifteen() -> Int {
    var y = 10;func add() {
        y += 5
    }
    add()
    return y
}

returnFifteen()

func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
var increment = makeIncrementer()
increment(7)

class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
